﻿/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Daidalos.Interfaces;

namespace Daidalos.UI
{

    /// <summary>
    ///     This class implements a sprite, being a transformed, colored and textured quad.
    /// </summary>
    public class Sprite : Interfaces.Cloneable<Sprite>
    {

        #region Attributes
        /// <summary>
        ///     The vertices that make up the sprite.
        /// </summary>
        private CustomVertex.TransformedColoredTextured[] vertices = null;
        #endregion

        #region Properties
        /// <summary>
        ///     Implements an accessor to the sprite's vertices.
        /// </summary>
        public CustomVertex.TransformedColoredTextured[] Vertices
        {
            get
            {
                return vertices;
            }
            set
            {
                value.CopyTo(vertices, 0);
            }
        }

        /// <summary>
        ///     Implements a selector for the top left vertex.
        /// </summary>
        public CustomVertex.TransformedColoredTextured TopLeft
        {
            get
            {
                return vertices[1];
            }
        }

        /// <summary>
        ///     Implements a selector for the bottom left vertex.
        /// </summary>
        public CustomVertex.TransformedColoredTextured BottomLeft
        {
            get
            {
                return vertices[0];
            }
        }

        /// <summary>
        ///     Implements a selector for the bottom right vertex.
        /// </summary>
        public CustomVertex.TransformedColoredTextured BottomRight
        {
            get
            {
                return vertices[2];
            }
        }

        /// <summary>
        ///     Implements a selector for the top right vertex.
        /// </summary>
        public CustomVertex.TransformedColoredTextured TopRight
        {
            get
            {
                return vertices[3];
            }
        }

        /// <summary>
        ///     Implements accessors to the sprite's width.
        /// </summary>
        public float Width
        {
            get
            {
                return vertices[2].X - vertices[0].X;
            }
            set
            {
                vertices[2].X = vertices[0].X + value;
                vertices[3].X = vertices[0].X + value;
            }
        }

        public float Height
        {
            get
            {
                return vertices[0].Y - vertices[1].Y;
            }
            set
            {
                vertices[0].Y = vertices[1].Y + value;
                vertices[2].Y = vertices[1].Y + value;
            }
        }

        /// <summary>
        ///     Implements accessors for the X-position of the sprite.
        /// </summary>
        public float Left
        {
            get
            {
                return vertices[0].X;
            }
            set
            {
                float width = Width;
                vertices[0].X = value;
                vertices[1].X = value;
                vertices[2].X = value + width;
                vertices[3].X = value + width;
            }
        }

        /// <summary>
        ///     Implements accessors for the Y-position of the sprite.
        /// </summary>
        public float Top
        {
            get
            {
                return vertices[1].Y;
            }
            set
            {
                float height = Height;
                vertices[0].X = value + height;
                vertices[1].X = value;
                vertices[2].X = value + height;
                vertices[3].X = value;
            }
        }

        /// <summary>
        ///     Implements a selector for the Y-Position of the
        ///     far right edge of the sprite.
        /// </summary>
        public float Right
        {
            get
            {
                return vertices[2].X;
            }
        }

        /// <summary>
        ///     Implements a selector for the Y-Position of the
        ///     bottom most edge of the sprite.
        /// </summary>
        public float Bottom
        {
            get
            {
                return vertices[2].Y;
            }
        }

        /// <summary>
        ///     Implements accessors to the colors of the sprite.
        /// </summary>
        public int Color
        {
            get
            {
                return vertices[0].Color;
            }
            set
            {
                vertices[0].Color = value;
                vertices[1].Color = value;
                vertices[2].Color = value;
                vertices[3].Color = value;
            }
        }
        #endregion

        #region Constructors
        /// <summary>
        ///     Creates an empty object.
        /// </summary>
        public Sprite()
        {
        }

        /// <summary>
        ///     Creates a sprite using the given screen coordinates.
        /// </summary>
        /// <param name="topLeft">
        ///     The top left vertex.
        /// </param>
        /// <param name="topRight">
        ///     The top right vertex.
        /// </param>
        /// <param name="bottomLeft">
        ///     The bottom left vertex.
        /// </param>
        /// <param name="bottomRight">
        ///     The bottom right vertex.
        /// </param>
        public Sprite(CustomVertex.TransformedColoredTextured topLeft, CustomVertex.TransformedColoredTextured topRight, CustomVertex.TransformedColoredTextured bottomRight, CustomVertex.TransformedColoredTextured bottomLeft)
        {
            vertices    = new CustomVertex.TransformedColoredTextured[4];
            vertices[0] = bottomLeft;
            vertices[1] = topLeft;
            vertices[2] = bottomRight;
            vertices[3] = topRight;
        }
        #endregion

        #region Methods
        /// <summary>
        ///     This method creates a string describing the type and
        ///      state of the object.
        /// </summary>
        /// <returns>
        ///     A string describing the object's type and state.
        /// </returns>
        public override string ToString()
        {
            return "Daidalos.UI.Sprite<x=" + vertices[0].X + ",y=" + vertices[0].Y + ",w=" + Width + ",h=" + Height + ">";
        }

        /// <summary>
        ///     This method clones the sprite object.
        /// </summary>
        /// <returns>
        ///     A reference to a similar sprite object.
        /// </returns>
        public Sprite Clone()
        {
            return new Sprite(vertices[1], vertices[3], vertices[2], vertices[0]);
        }
        #endregion

    }

}
